using System.Net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using blog.BLL;
using blog.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace blog.Controllers {
    public class UserController : BaseController {
        [HttpGet]
        public ActionResult index (int pageNum = 1,int pageSize = 5) {
            List<UsersModel> re = UserBLL.getPageList (pageNum, pageSize);
            ViewBag.users = re;
            ViewBag.total = UserBLL.totalCount();
            ViewBag.pageNum = pageNum;
            ViewBag.pageSize = pageSize;
            ViewBag.pageTotal = Math.Ceiling(Convert.ToDecimal(ViewBag.total) / Convert.ToDecimal(ViewBag.pageSize));
            return View ();
        }

        public ActionResult add() {
            return View();
        }

        public ActionResult edit(int id) {
            UsersModel user = UserBLL.findById(id);
            ViewBag.user = user;
            return View();
        }

        public ActionResult editAjax(int id, string account, string name, string password, string password2) {
            UsersModel user = UserBLL.findById(id);
            if(user != null) {
                user.account = account;
                user.name = name;
                if(password != null && password2 != null) {
                    if(password.Equals(password2)) {
                        user.password = password;
                    } else {
                        return result(null, "两次输入密码不一致", ResultModel.CODE_FAIL);
                    }
                }
                bool re = UserBLL.saveUser(user);
                if(re == true) {
                    return result("/user/index");
                }
                return result(null, "编辑失败", ResultModel.CODE_FAIL);
            }

            return result(null, "用户信息错误", ResultModel.CODE_FAIL);
        }

        public ActionResult addAjax (string name, string password, string password2, string account) {
            if(name == null || password == null || account == null) {
                return result(null, "缺少必填参数", ResultModel.CODE_FAIL);
            }
            if(name.Trim() == "" || password.Trim() == "" || account.Trim() == "") {
                return result(null, "缺少必填参数", ResultModel.CODE_FAIL);
            }
            if(password.Equals(password2) == false) {
                return result(null, "两次输入密码不一致", ResultModel.CODE_FAIL);
            }
            UsersModel user = new UsersModel ();
            user.name = name.Trim();
            user.password = password.Trim();
            user.account = account.Trim();
            if (UserBLL.addUser (user)) {
                return result ("/user/index");
            } else {
                return result (null, "保存失败", ResultModel.CODE_FAIL);
            }

        }

        [HttpPost]
        public ActionResult deleteAjax(int id) {
            if(UserBLL.deleteUser(id) == true) {
                return result("/user/index");
            }
            return result(null, "删除失败", ResultModel.CODE_FAIL);
        }
    }
}